package com.heima.wemedia.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.model.wemedia.pojos.WmNewsMaterial;
import com.heima.wemedia.mapper.WmMaterialMapper;
import com.heima.wemedia.mapper.WmNewsMaterialMapper;
import com.heima.wemedia.service.WmNewsMaterialService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * @author: zhk
 * @since: 2025/7/3
 */
@Slf4j
@Service
@Transactional
public class WmNewsMaterialServiceImpl extends ServiceImpl<WmNewsMaterialMapper, WmNewsMaterial> implements WmNewsMaterialService {
   @Autowired
    private WmMaterialMapper wmMaterialMapper;
    @Autowired
    private WmNewsMaterialMapper wmNewsMaterialMapper;
    @Override
    public void deleteByNewsId(Long NewsId) {
        LambdaQueryWrapper<WmNewsMaterial> lqw = new LambdaQueryWrapper<>();
        lqw.eq(WmNewsMaterial::getNewsId,NewsId);
        remove(lqw);

    }

    @Override
    public void insertImage(List<String> images, Long newsId, Short type) {

        log.info(images.toString());
        if (images.size() == 0) {
            return;
        }
        //去除每个元素的前后空格
        List<String> imagesNews = new ArrayList<>(images.size());
        for (String image : images) {
            imagesNews.add(image.trim());
        }
        List<Long> materialIds = wmMaterialMapper.selectMaterialIdByUrl(imagesNews);
        log.info(materialIds.toString());

        //遍历插入
//        for (int i = 0; i < materialIds.size(); i++) {
//            WmNewsMaterial wmNewsMaterial = new WmNewsMaterial();
//            Long materialId = materialIds.get(i);
//
//            wmNewsMaterial.setMaterialId(materialId);
//            wmNewsMaterial.setNewsId(newsId);
//            wmNewsMaterial.setType(type);
//            wmNewsMaterial.setOrd((short)i);
//
//            log.info("存入中间表数据"+wmNewsMaterial);
//            save(wmNewsMaterial);
//        }
        //批量插入
        wmNewsMaterialMapper.insertAll(materialIds, newsId, type);

    }
}
